// [2444]统计定界子数组的数目
struct Solution;

//leetcode submit region begin(Prohibit modification and deletion)
impl Solution {
    pub fn count_subarrays(nums: Vec<i32>, min_k: i32, max_k: i32) -> i64 {
        let mut ans = 0i64;
        let mut min_index = -1;
        let mut max_index = -1;
        let mut border = -1;
        for (i, &num) in nums.iter().enumerate() {
            if num == min_k {
                min_index = i as i32;
            }
            if num == max_k {
                max_index = i as i32;
            }
            if num < min_k || num > max_k {
                min_index = -1;
                max_index = -1;
                border = i as i32;
            }
            if min_index != -1 && max_index != -1 {
                ans = ans + (min_index.min(max_index) - border) as i64;
            }
        }
        ans
    }
}
//leetcode submit region end(Prohibit modification and deletion)

/// 总结：
/// 使用自然语言或伪代码推算后，在进行代码编写

#[test]
fn main() {
    let nums = vec![1, 3, 5, 2, 7, 5];
    assert_eq!(Solution::count_subarrays(nums, 1, 5), 2);
    let nums = vec![1, 1, 1, 1];
    assert_eq!(Solution::count_subarrays(nums, 1, 1), 10);
}
